//应收账单管理/账单管理
//杨帆

interface IReceivableBillManagentViewModel_managent extends IBaseViewModel<IReceivableBillManagentQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceReceivableListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 开票状态下拉
     */
    invoiceManagentStatusDropDown:ValueListItemResponse[];
    /**
     * id
     */
    receivableId:string;
    /**
     * 账单管理详情
     */
    receiptManangentData:ReceivableSettleBillDetailResponse;
    /**
     * 账单管理--确定
     */
    manangentSave:()=>void;
    /**
     * 撤出账单
     */
    receivablDel:(id:string)=>void;
    /**
     * 删除新增的应收
     */
    deleteRec:(orderId:string,id:string)=>void;
    /**
     * 查看详情
     */
    receivablDetail:(carrierOrderId:string,orderId:string,orderChildId:string,id:string,receivableCode:string)=>void;
    /**
     * 新增应收订单
     */
    skipAddReceicale:()=>void;
    /**
     * 导出 显示/隐藏
     */
    exportDis:boolean;
    
    /**
     * 结算状态集合
     */
    settlestatuslist:string;
    /**
     * 回单状态
     */
    receiptstatus:string;
    /**
     * 开票状态
     */
    Status:string;
    /**
     * 列表高
     */
    heightA:number;

    back:()=>void
  
}

/**
 * 查询条件
 */
interface IReceivableBillManagentQueryTerms{
    /**
     * 应收编号
     */
    receivableBillId:string;
    /**
     * 合同号
     */
    contractId:string;
    /**
     * 客户单位
     */
    clientName:string
}

class IReceivableBillManagentController_managent extends ListController<IReceivableBillManagentViewModel_managent,IReceivableBillManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<FinanceReceivableListItemResponse>;
    orderBy:number = null ;
    settlestatuslist="";
    Status="";

    constructor($scope:IReceivableBillManagentViewModel_managent,private receivableService:IReceivableService, private $ngBootbox: BootboxService,
    private valueService: IValueService,private authorizationService:IAuthorizationService,private $location:ng.ILocationService,private orderService:IorderService,
    private $state: angular.ui.IStateService,private exportService:IexportService){
        super($scope);
        this.init();
    }

    init():void{
        super.init();
        
        this.$scope.receiptstatus = "";

        this.$scope.queryTerms = {
            receivableBillId:"",
            contractId:"",
            clientName:""
        };

        $('#openinvoiceTime').click(()=> {
        $('#invoiceTime').datetimepicker('show');
        });
        //总单编号
        this.$scope.receivableId = this.$location.search().id;
              
        this.$scope.gridOptions = {
            enableSelectAll: false,//全选禁用
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: false,
            enableColumnResizing: true,
            enableSorting: false,
            columnDefs:[
                { displayName: "应收编号", field: 'receivableCode', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: "应收费用类型", field: 'feeType', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: "应收说明", field: 'receivableSummary', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '客户单位', field: 'client', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '结算单位', field: 'settle', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '发货地址', field: 'originAddress', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '发货日期', field: 'realDeliveryTime', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '发货时间', field: 'realDeliveryTime', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '应收总价', field: 'receivableTotalPrice', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '合同号', field: 'contractNumber', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '结算状态', field: 'settleStatus', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { displayName: '回单状态', field: 'receiptStatus', enableColumnMenu: false, cellTooltip:true, headerTooltip: true },
                { 
                    displayName: "操作",
                            field: 'orderStatus',
                            cellTemplate: '<div ng-if="row.entity.isIndependent == false && row.entity.invoiceStatus == \'未开票\' ">'
                                            + '<a class="primary fa fa-undo text-danger m-l-xs" title="撤出" ng-click="grid.appScope.receivablDel(row.entity.id)" ></a>'
                                            + '<a class="fa fa-eye m-l-xs  primary text-info" title="查看详情" ng-click="grid.appScope.receivablDetail(row.entity.carrierOrderId,row.entity.orderId,row.entity.orderChildId,row.entity.id,row.entity.receivableCode)" ></a>'
                                          + '</div>'
                                          + '<div ng-if="row.entity.isIndependent == true ">'
                                            + '<a class="primary fa fa-trash-o text-danger m-l-xs" title="删除" ng-click="grid.appScope.deleteRec(row.entity.orderId,row.entity.id)" ></a>'
                                            + '<a class="fa fa-eye m-l-xs  primary text-info" title="查看详情" ng-click="grid.appScope.receivablDetail(row.entity.carrierOrderId,row.entity.orderId,row.entity.orderChildId,row.entity.id,row.entity.receivableCode)" ></a>'
                                          + '</div>', 
                           enableColumnMenu: false, 
                           enableSorting: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };

        this.query();
        this.loadInvoiceManagentStatusDropDown();
        this.$scope.manangentSave = this.manangentSave;
        this.$scope.receivablDel = this.receivablDel;
        this.$scope.deleteRec = this.deleteRec;
        this.$scope.receivablDetail = this.receivablDetail;
        this.getDetail();
        this.$scope.skipAddReceicale = this.skipAddReceicale;

        this.$scope.back = this.back;

        /**
         * 应收对账导出权限值
         */
        this.$scope.exportDis = false;
        this.authorizationService.auth('25B5C4744B62B248E85CFABA3575091FE75E7393360C8CBEC5BC82E3A95B1F6C').then((res)=>{
            this.$scope.exportDis = res.data.extData;
        })
                
    }

    /**
     * 加载数据
     */
    loadData(queryTerms:IReceivableBillManagentQueryTerms,skip:number,count:number):void{
        this.receivableService.receivablelist("",null, this.queryTerms.clientName ,"","","","","","", this.queryTerms.contractId ,"","","","",this.$scope.receivableId,null,this.queryTerms.receivableBillId,"",false, 
        "" ,"","","","","",skip,count).then((res)=>{
            // this.$scope.gridOptions.totalItems = res.data.total;
            this.$scope.gridOptions.data = res.data.data;
            this.$scope.heightA = (this.$scope.gridOptions.data.length * 39)+95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            }); 
    }
    
    getListCount():void{
         this.receivableService.receivablecount(this.$scope.queryTerms.clientName ,"","","","","","", this.$scope.queryTerms.contractId ,"","","","",this.$scope.receivableId,null,this.$scope.queryTerms.receivableBillId,"",false,"" ,"","","","","").then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }

    // localHistory():void{
    //     super.localHistory(this.$state);
    // }
    // getHistory():void{
    //     super.getHistory(this.$state,this.$location)
    // }


    /**
     * 断言
     */
    isReceivableManagentListResponse(response: string | FinanceReceivableListItemResponse[]): response is FinanceReceivableListItemResponse[] {
        return (<FinanceReceivableListItemResponse[]>response).forEach !== undefined;
    }

    /**
     * 获取账单管理数据
     */
    getDetail =()=> {
        this.receivableService.getReceivableSettleBillDetail(this.$scope.receivableId).then((res)=>{
            this.$scope.receiptManangentData = res.data;
            this.$scope.Status = res.data.invoiceStatus;
        });
    }
        
    /**
     * 开票状态下拉
     */
    loadInvoiceManagentStatusDropDown():void{
        this.$scope.invoiceManagentStatusDropDown = this.valueService.getInvoiceStatusList().data ;
    }

    /**
     * 保存账单对账
     */
    manangentSave = ()=> {
        this.$ngBootbox.confirm("确定保存吗？").then((res)=>{
            this.receivableService.checked(this.$scope.receivableId,this.$scope.receiptManangentData.invoiceStatus,$("#invoiceTime").val()).then((result)=>{
                this.$ngBootbox.alert("保存成功");
                this.query();
                this.getDetail();
            });
        });
        
    }

    /**
     * 撤出账单
     */
    receivablDel =(id:string)=> {
        this.$ngBootbox.confirm('确定撤出账单吗?').then((res)=>{
            this.receivableService.withdrawnbill(id).then((res)=>{
                this.query();
                this.getDetail();
            });    
        });
    }

    /**
     * 删除新增的应收
     */
    deleteRec =(orderId,id)=> {
        this.$ngBootbox.confirm("确定删除账单吗?").then((res)=>{
            this.orderService.delReceivableByOrderId(orderId,id).then((res)=>{
                this.$ngBootbox.alert("删除成功");
                this.query();
                this.getDetail();
            });
        });
    }


    /**
     * 查看详情
     */
    receivablDetail = (carrierOrderId:string,orderId:string,orderChildId:string,id:string,receivableCode:string)=> {
        this.$state.go('app.receivable.receivable_reconcile',{ carrierOrderId:carrierOrderId, orderId:orderId, orderChildId:orderChildId, Id:id, receivableCode:receivableCode, name:'BillManage' });
    }


    /**
     * 新增应收订单
     */
    skipAddReceicale =()=> {
        this.$state.go("app.receivable.receivable_order_add", { id: this.$scope.receivableId });
    }

    //返回
    back=()=>{
        this.$state.go("app.receivable.receivableBill", { name:'receivableBill' });
    }
    


    /**
     * 导出列表
     */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportReceivable(this.orderBy,null,this.queryTerms.clientName,"","","","","","",
            this.queryTerms.contractId,"","","","",this.$scope.receivableId,null,this.queryTerms.receivableBillId,
            "",false,"","","","",null,"","");
        }
    }

}

angular.module("tms").controller("receivableBillManagentController", ["$scope", "receivableService", "$ngBootbox", "valueService" , "authorizationService", "$location" ,
 "orderService", "$state" ,"exportService", IReceivableBillManagentController_managent]);